package com.example.forum2.Controller;

import com.example.forum2.Service.impl.UserServiceImpl;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.config.AppConfig;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.Board;
import com.example.forum2.model.User;
import com.example.forum2.utils.MD5Util;
import com.example.forum2.utils.StringUtil;
import com.example.forum2.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("/register")
    public AppResult register(String username, String nickname, String password, String passwordRepeat){
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(nickname)
        ||StringUtil.isEmpty(password)||StringUtil.isEmpty(passwordRepeat)){
            throw  new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        User user = new User();
        user.setUsername(username);
        user.setNickname(nickname);
        if(!password.equals(passwordRepeat)){
       throw  new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        String salt = UUIDUtil.UUID_32();
        String encryptPassword = MD5Util.md5Salt(password, salt);
        // 2.3 设置密码
        user.setPassword(encryptPassword);
        user.setSalt(salt);
        userService.CreateNomalUser(user);
        return AppResult.success(user);

    }
    @RequestMapping("/login")
    public AppResult login(HttpServletRequest httpServletRequest,String username, String password){
        User user=userService.login(username,password);
        if(user==null){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        HttpSession httpSession = httpServletRequest.getSession(true);
        httpSession.setAttribute(AppConfig.USER_SESSION,user);
        return AppResult.success();
    }
    @RequestMapping("/logout")
    public AppResult logout (HttpServletRequest request){
        HttpSession session=request.getSession();
        if(session!=null){
            session.invalidate();
        }
     return AppResult.success();
    }
    @RequestMapping("/info")
    public AppResult<User> getUserInfo (HttpServletRequest request,  Long id) {
        User user=null;
        if(id==null){
            HttpSession session = request.getSession();
            if(session==null||session.getAttribute(AppConfig.USER_SESSION)==null){
                throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
            }
            user=(User) session.getAttribute(AppConfig.USER_SESSION);
        }else {
         user=userService.selectById(id);
        }
        return AppResult.success(user);
    }

    @RequestMapping("/allNormal")
    public AppResult<List<Board>> allNormal (){
        List<Board> boards = userService.selectAllNormal();
        return AppResult.success(boards);
    }
    @RequestMapping("/modifyInfo")
public AppResult modifyInfo(@SessionAttribute(AppConfig.USER_SESSION) User user, String username, String nickname, Byte gender, String email, String phoneNum, String remark){
       if(StringUtil.isEmpty(username)&&StringUtil.isEmpty(nickname)&&StringUtil.isEmpty(email)&&StringUtil.isEmpty(phoneNum)
       &&StringUtil.isEmpty(remark)&&gender==null){
           return AppResult.failed("请输入要修改的内容");

       }
       User updateUser = new User();
       updateUser.setId(user.getId());
       updateUser.setUsername(username);
       updateUser.setNickname(nickname);
       updateUser.setGender(gender);
       updateUser.setEmail(email);
       updateUser.setRemark(remark);
       updateUser.setPhoneNum(phoneNum);
       userService.modifyInfo(updateUser);
       return AppResult.success();

}
@RequestMapping("/modifyPwd")
public AppResult modifyPassword(HttpServletRequest request, String oldPassword, String newPassword, String passwordRepeat, HttpSession httpSession){
        if(!newPassword.equals(passwordRepeat)){
            return AppResult.failed(ResultCode.FAILED);
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        userService.modifyPassword(user.getId(),newPassword,oldPassword);
        return AppResult.success();
}
}
